<template>
  <div class="login-container">
    <div class="logo">
      <div class="icon"></div>
      <!-- <img src="@/assets/common/bg.png" width="800" style="max-height: 100vh;" alt="" /> -->
    </div>
    <div class="form" :class="{ 'centered-form': isMobile }">
      <el-card class="login-card">
        <el-form ref="loginForm" :model="loginForm" class="login-form" auto-complete="on" label-position="left">
          <div class="title-container">
            <h3 class="title">登录</h3>
          </div>

          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input ref="username" v-model="loginForm.username" placeholder="Username" name="username" type="text"
              tabindex="1" auto-complete="on" />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
              placeholder="Password" name="password" tabindex="2" auto-complete="on" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-button type="primary" style="width: 100%; margin-bottom: 30px"
            @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      isMobile: false // Add a flag to detect mobile
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.isMobile = this.detectMobile() // Detect mobile on mount
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async handleLogin() {
      await this.$store.dispatch('user/login', this.loginForm)
      this.$router.push('/')
    },
    detectMobile() {
      const userAgent = navigator.userAgent
      return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent)
    }
  }
}
</script>

<style lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  // background-color: #cfdbf4;
  background-image: url('../../assets/common/bg.png');
  background-size: 100vw 100vh;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;
    }
  }

  .el-button {
    background-color: #5171b9;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #454545;
    position: relative;

    .getVcode {
      position: absolute;
      top: 10px;
      right: 10px;
      opacity: 0.9;
    }
  }
}

.logo {
  flex: 3;
  // background: url(../../assets/common/logBg.png) no-repeat center / cover;
  border-top-right-radius: 60px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  padding: 0 100px;

  p {
    color: #fff;
    font-size: 18px;
    margin-top: 20px;
    width: 300px;
    text-align: center;
  }
}

.form {
  flex: 2;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 50px;

  .el-card {
    border-radius: 30px;
  }

  .ant-card {
    width: 320px;
    border: none;
    padding: 0;
  }

  .login-form {
    position: relative;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  h3 {
    padding-left: 30px;
    font-size: 24px;
  }

  // Style for centering form on mobile
  &.centered-form {
    align-items: center;
    padding: 20px;
  }
}
</style>
